﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OBDUDSUtils.Common;
using OBDUDSUtils;
using OBDUDSUtils.CNNMode;

namespace OBDUDSUtils.uds
{
    class UdsDiag
    {
        public delegate void OBDMessage(List<DiagEntity> results, DiagBasic.ModelSID mode);
        public event OBDMessage OBDMessageEvent;
        private List<string> resultDid = new List<string>();//第一个byte为数据长度，后两个BYTE为DID 
        private Queue<DidResultEntity> didResultEntities;
        private bool currentDidCompleted;

        public UdsDiag(DiagData data)
        {
            //CANDevice.InitCANDevice(ref data);
            //CANDevice.NoticeMessageEvents += _CANDevice_NoticeMessageEvents;
        }

        public void SendOBDMessage(List<DiagEntity> results, DiagBasic.ModelSID mode)
        {
            OBDMessageEvent(results, mode);
        }

        public void OpenCAN()
        {
            //CANDevice.OpenCAN();
        }

        public void CloseCAN()
        {
            //CANDevice.CloseCAN();
        }

        private void _CANDevice_NoticeMessageEvents(List<CANBasic.VCI_CAN_OBJ> data)
        {
            foreach (var frame in data)
            {
                UpdateDidResult(frame.Data, 0x62);
            }
        }

        private void UpdateDidResult(byte[] didData, byte dstByte)
        {
            try
            {
                //10 14 62 F1 90 3F 3F 3F
                int dataLen = 0;//单/多帧数据长度
                if (this.resultDid.Count() > 0)
                {
                    dataLen = Convert.ToInt32(this.resultDid[0], 16);
                }
                var data1 = didData[0].ToString().PadLeft(2, '0');
                if (data1.Substring(0, 1) == "0")//单帧,直接解析数据
                {
                    if (didData[1] == dstByte)
                    {
                        this.resultDid.Clear();
                        dataLen = didData[0];
                        this.resultDid.Add(didData[0].ToString("X2"));
                        for (int i = 2; i < didData[0]; i++)
                        {
                            if (this.resultDid.Count() < dataLen)
                            {
                                this.resultDid.Add(didData[i].ToString("X2"));
                            }
                        }
                    }
                }
                else if (data1.Substring(0, 1) == "1")//多帧，需要发送30帧
                {
                    if (didData[2] == dstByte)//开始解析目标数据
                    {
                        this.resultDid.Clear();
                        dataLen = didData[1];
                        this.resultDid.Add(didData[1].ToString("X2"));
                        for (int i = 3; i < didData.Length; i++)
                        {
                            this.resultDid.Add(didData[i].ToString("X2"));
                        }
                        //CANDevice.SendCANMessage(0x7e0, new byte[] { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, false, 0);
                    }
                }
                else//解析多帧数据
                {
                    for (int i = 1; i < didData.Length; i++)
                    {
                        if (this.resultDid.Count() < dataLen)
                        {
                            this.resultDid.Add(didData[i].ToString("X2"));
                        }
                        else
                        {
                            break;
                        }
                    }

                }

                if (this.resultDid.Count() == dataLen)//当前解析完成
                {
                    LogHelper.Log.Info("DID解析已完成");
                    //this.didResultEntities.Enqueue(new DidResultEntity() { DidResults = this.resultDid, DataLength = dataLen, DIDString = this.resultDid[1] + this.resultDid[2] });
                    //this.currentDidCompleted = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error(ex.Message + ex.StackTrace);
            }
        }
    }
}
